package com.dreamlike.nlp.DAO;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DatabasePool {
    private MySQLPool pool;

    @Autowired
    public DatabasePool(MySQLConnectOptions mySQLConnectOptions, Vertx vertx, PoolOptions poolOptions) {
        this.pool = MySQLPool.pool(vertx, mySQLConnectOptions, poolOptions);
    }

    public Future<SqlConnection> getConnect(){
        Promise<SqlConnection> promise = Promise.promise();
        pool.getConnection(ar -> {
            if (ar.succeeded()) {
                promise.complete(ar.result());
            }else {
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }


}
